package com.bike.controller.information;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.httpclient.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.bike.controller.BaseController;
import com.bike.entity.HhFoodMenu;
import com.bike.service.impl.HhFoodMenuServiceImpl;
import com.bike.util.Constant;
import com.bike.util.ExcelUtil;
import com.bike.util.PageData;
import com.bike.util.TimeManager;
import com.bike.util.Tools;
import com.bike.util.UuidUtil;
import com.sun.org.apache.xpath.internal.operations.Mod;
import com.sun.xml.internal.bind.v2.runtime.reflect.opt.Const;

import jodd.upload.FileUpload;

 
@Controller
@RequestMapping(value="/hhFoodMenu")
public class HhFoodMenuController extends BaseController {
	
		@Autowired
		private HhFoodMenuServiceImpl  hhFoodMenuService;
	
		/**
		 * 跳转到列表页面
		 */
		@RequestMapping("toList")
		public String toList(){
			return "/information/foodMenu/hhFoodMenu_list";
		}
		/**
		 * 列表查询(或根据条件)
		 */
		@RequestMapping("list")
		@ResponseBody
		public PageData list(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			return hhFoodMenuService.selectHhFoodMenuList(pd);
		}
		/**
		 * 列表查询(或根据条件)
		 */
		@RequestMapping("listAll")
		@ResponseBody
		public PageData listAll(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			return hhFoodMenuService.selectHhFoodMenuList(pd);
		}
		/**
		 * 跳转到编辑页面（新增和修改）
		 */
		@RequestMapping("toEdit")
		public String toEdit(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			model.addAttribute("pd",pd);
			if(pd.getLong("foodMenuId")>0){
				model.addAttribute("pd", hhFoodMenuService.selectByHhFoodMenuId(pd.getLong("foodMenuId")));
			} 
			return "/information/foodMenu/hhFoodMenu_edit";
		}
		/**
		 * 搜素
		 * @param request
		 * @param model
		 * @return
		 */
		@RequestMapping("toSearch")
		public String toSearch(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			model.addAttribute("pd", pd);
			return "/information/foodMenu/hhFoodMenu_search";
		}
		/**
		 * 新增和修改 
		 */
		@RequestMapping("save")
		@ResponseBody
		 public Map<String,Object> save(HttpServletRequest request,HhFoodMenu hhFoodMenu){
			try {
				PageData pd=PageData.getPageDataByPage(request);
				hhFoodMenuService.saveOrUpdate(pd,hhFoodMenu);
				return getSuccessMap();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return getErrorMap("失败");
		 }
		/**
		 * 删除
		 */
	 	@RequestMapping("delete")
		@ResponseBody
		 public Map<String,Object> delete(HttpServletRequest request){
			try {
				PageData pd=PageData.getPageDataByPage(request);
				if(pd.getLong("foodMenuId")>0){
					hhFoodMenuService.deleteHhFoodMenuById(pd.getLong("foodMenuId"));
					return getSuccessMap();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return getErrorMap("失败");
		}
	 	
	 	/**
		 * 导出到excel
		 */
		@RequestMapping(value = "/excel")
		public void exportExcel(HttpServletRequest request,HttpServletResponse response) {
			PageData pd = PageData.getToPageData(request);
			try {
				response.reset();
				response.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode("foodMenu.xls", "UTF-8"));
				response.setHeader("Connection", "close");
				response.setHeader("Content-Type", "application/octet-stream");
						
				OutputStream ops = null;
				FileInputStream fis =null;
				byte[] buffer = new byte[8192];
				int bytesRead = 0;
				String realPath = request.getSession().getServletContext().getRealPath("");
				try {
				    ops = response.getOutputStream();
				    fis = new FileInputStream(realPath.concat("/static/excel/food_menu.xls"));
				    while((bytesRead = fis.read(buffer, 0, 8192)) != -1){
				         ops.write(buffer, 0, bytesRead);
				    }
				    ops.flush();
				    } catch (IOException e) {
					e.printStackTrace();
				    } finally {
					try {
					    if(fis != null){
						fis.close();
					    }
					    if(ops != null){
						ops.close();
					    }
					} catch (IOException e) {
					    e.printStackTrace();
					}
				}
			} catch (Exception e) {
				logger.error(e.toString(), e);
			}
		}

		/**
		 * 打开上传EXCEL页面
		 */
		@RequestMapping(value = "/toUploadExcel")
		public String toUploadExcel(HttpServletRequest request,Model model) throws Exception {
			PageData pd = PageData.getToPageData(request);
			try {
				model.addAttribute("pd", pd);
			} catch (Exception e) {
				logger.error(e.toString(), e);
			}
			return "/information/foodMenu/uploadexcel";
		}

		@RequestMapping("/readExcel") 
		@ResponseBody
		public PageData readExcel(@RequestParam(value = "excel", required = false) MultipartFile file, HttpServletResponse response, HttpServletRequest request) throws Exception {
			PageData pd = PageData.getToPageData(request);
			response.setContentType("text/html;charset=utf-8");
			request.setCharacterEncoding("utf-8");
			if (null != file && !file.isEmpty()) {
				
				List<List<Object>> listPd = ExcelUtil.getExcelInfo(file.getInputStream(), pd,file.getOriginalFilename());
				 
				pd.put("addTime", TimeManager.currentTime());
				pd.put("foodMenus", listPd);
				 hhFoodMenuService.insertBatch(pd);
			}
			return getSuccessPd();
		}

}
